//Usage:
// {
//   "groupid" : "",
//   "personid": "",
//   "img": ""
// }
// 云函数入口文件
const cloud = require('wx-server-sdk')
var utilMd5 = require('md5.js')

cloud.init()

var verifyPerson = function(event) {

  const tencentcloud = require("tencentcloud-sdk-nodejs");

  const IaiClient = tencentcloud.iai.v20180301.Client;
  const models = tencentcloud.iai.v20180301.Models;

  const Credential = tencentcloud.common.Credential;
  const ClientProfile = tencentcloud.common.ClientProfile;
  const HttpProfile = tencentcloud.common.HttpProfile;

  let cred = new Credential("AKIDa3PWXQYgBH6lGdoMo9mIratAJNLEtuX1", "ZM0ZZedXO7Vs1iQ2d8biJ1JbpHdtaCQH");
  let httpProfile = new HttpProfile();
  httpProfile.endpoint = "iai.tencentcloudapi.com";
  let clientProfile = new ClientProfile();
  clientProfile.httpProfile = httpProfile;
  let client = new IaiClient(cred, "ap-guangzhou", clientProfile);

  let req = new models.VerifyFaceRequest();

  let params = `{"Image":"${event.img}","PersonId":"${event.personid}"}`
  req.from_json_string(params);

  return new Promise((resolve, reject) => {
    client.VerifyFace(req, function(errMsg, response) {
      if (errMsg) {
        console.log(errMsg);
        event.result = "fail"
        // reject(event)
        // errMsg.event = event
        reject(errMsg)
      } else {
        console.log(response.to_json_string());
        console.log(response.IsMatch)
        if (response.IsMatch) {
          event.result = true

          var curtime = Date.now() + '';

          var curtime_sub_str = curtime.substring(0, 6);
          console.log(curtime_sub_str)

          var str = event.groupid + event.personid + curtime_sub_str;
          console.log(str)

          var md5string = utilMd5.hexMD5(str)
          event.md5string = md5string
          console.log(md5string)

        } else {
          event.result = false
        }
        // resolve(event)
        // response.event = event
        resolve(response)
      }
    })
  })

  // client.VerifyFace(req, function (errMsg, response) {
  //   if (errMsg) {
  //     console.log(errMsg);
  //     event.result = "fail"
  //     return
  //   }
  //   console.log(response.to_json_string());
  //   console.log(response.IsMatch)
  //   if(response.IsMatch){
  //     event.result = "success"

  //     var curtime = Date.now() + ''; 

  //     var curtime_sub_str = curtime.substring(0, curtime.length - 5);
  //     console.log(curtime_sub_str)

  //     var str = event.groupid + event.personid + curtime_sub_str;
  //     console.log(str)

  //     var md5string = utilMd5.hexMD5(str)
  //     event.md5string = md5string
  //     console.log(md5string)

  //   }else{
  //     event.result = "fail"
  //   }
  // });
}

// 云函数入口函数
exports.main = async(event, context) => {
  const wxContext = cloud.getWXContext()
  console.log("begin")
  // verifyPerson({
  //   event,
  //   success: res => {

  //   },
  //   fail: err =>{

  //   }
  // })
  var ret = await verifyPerson(event)
  event.img = ""
  console.log(event)
  return event
}